/*
 * Copyright (c) 2012 Mikaël GUILLEMOT
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.g2de.tools;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

/**
 * classe d'outils pour la manimulation et la preparation des images du jeux.
 * (pour le moment outils statiques.)
 * 
 * @author Mikael GUILLEMOT
 * @version 1.0
 * @since
 * 
 */
public class GraphicsTools {

	/**
	 * methode permetant de redimentionner une BufferedImage
	 * 
	 * @param src
	 *            l'image source
	 * @param neww
	 *            la nouvelle largeur (x)
	 * @param newh
	 *            la nouvelle hauteur (y)
	 * @return
	 */
	public static BufferedImage scale(BufferedImage src, int neww, int newh) {

		int w = src.getWidth();
		int h = src.getHeight();

		BufferedImage dest = new BufferedImage(neww, newh, src.getType());
		Graphics2D g = dest.createGraphics();
		if (w + h < neww + newh)
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		else
			g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		// TODO ici verifier qu'on est bien correct (en float)
		AffineTransform at = AffineTransform.getScaleInstance(neww / w, newh / h);
		g.drawRenderedImage(src, at);
		return dest;
	}
}
